#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-08-21 5:27 下午
# @Author  : yangdy
# @File    : 2020-08-21.111.二叉树的最小深度
# @Software: PyCharmCE
# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def minDepth(self, root: TreeNode) -> int:
        if not root:
            return 0
        lh = self.minDepth(root.left)
        rh = self.minDepth(root.right)
        ans = 0
        if lh > 0 and rh > 0:
            ans = min(lh, rh) + 1
        elif lh > 0:
            ans = lh + 1
        elif rh > 0:
            ans = rh + 1
        else:
            ans = 1
        return ans
